package com.jiangxfei.mymvp.app.home.mvp;

import com.jiangxfei.mymvp.app.home.bean.CarBean;
import com.jiangxfei.mymvp.base.BaseModel;
import com.jiangxfei.mymvp.config.APIService;
import com.jiangxfei.mymvp.http.net.CarResult;
import com.jiangxfei.mymvp.http.net.OnLoadCallback;
import com.jiangxfei.mymvp.util.LogUtil;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author: JiangXFei
 * @date: 2018/1/9 0009
 * @content:
 */

public class HomeModel extends BaseModel implements HomeConstract.LoadAllModel {

    private APIService apiService;

    public HomeModel(APIService apiService) {
        this.apiService=apiService;
    }

    @Override
    public void loadAll(OnLoadCallback callback) {
        apiService
                .loadAll()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CarResult<List<CarBean>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CarResult<List<CarBean>> listHttpResult) {
                        callback.onSuccess(listHttpResult.getTwoKilometreParkList(), listHttpResult.getMessage(), listHttpResult.getStatus());
                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtil.e(e.toString());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
